package com.itmk.web.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import itmk.utils.ResultUtils;
import itmk.utils.ResultVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import com.itmk.web.user.entity.User;
import com.itmk.web.user.entity.UserParm;
import com.itmk.web.user.service.UserService;

/**
*员工管理的控制器
*/
@RestController
@RequestMapping("api/user")
public class UserController {

    @Autowired
    private UserService userService;
    /**
     *新增员工
     */
    @PostMapping
    public ResultVo addUser(@RequestBody User user){
        //判断登录名是否重复
        if (StringUtils.isNotEmpty(user.getLoginName())){
            QueryWrapper<User> query=new QueryWrapper<>();
            query.lambda().eq(User::getLoginName,user.getLoginName());
            User one = userService.getOne(query);
            if (one!=null){
                return ResultUtils.error("用户登录名不能重复",500);
            }
        }
        //如果密码存在。MD5加密
        if (StringUtils.isNotEmpty(user.getUserName())){
            user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        }
        boolean save = userService.save(user);
        if(save){
            return ResultUtils.success("新增员工成功！");
        }
        return ResultUtils.error("新增员工失败!");
    }

    /**
     * 编辑员工
     */
    @PutMapping
    public ResultVo editUser(@RequestBody User user){
        //判断登录名是否重复
        if(StringUtils.isNotEmpty(user.getLoginName())){
            QueryWrapper<User> query=new QueryWrapper<>();
            query.lambda().eq(User::getLoginName,user.getLoginName());
            User one = userService.getOne(query);
            if (one!=null && !one.getUserId().equals(user.getUserId())){
                return ResultUtils.error("登录名已经占用",500);
            }
        }
        //如果密码存在。MD5加密
        if (StringUtils.isNotEmpty(user.getUserName())){
            user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        }
        boolean b = userService.updateById(user);
        if(b){
            return ResultUtils.success("编辑员工成功！");
        }
        return ResultUtils.error("编辑员工失败");
    }
    /**
     * 删除员工
     */
    @DeleteMapping("/{userId}")
    public ResultVo deleteUser(@PathVariable("userId") Long userId){
        boolean b = userService.removeById(userId);
        if (b){
            return ResultUtils.success("删除员工成功");
        }
        return ResultUtils.error("删除员工失败");
    }

    /**
     * 查询员工列表
     * @param parm
     * @return
     */
    @GetMapping("/list")
    public ResultVo list(UserParm parm){
        IPage<User> list = userService.list(parm);
        list.getRecords().stream().forEach(item -> item.setPassword(""));
        return ResultUtils.success("查询成功",list);
        }
}
